<?php
namespace app\common\tool\profit_sharing;

class ShareholderMultiple extends Multiple {
	//分账
	public function share($order) {
        $user = loadModel('tyyh\\User')->find($order['uid']);
        $spreadid = $user['spreadid'];
		$system = config('wechat.system');
        $receivers = [];
        $commission = $order['commission'] * 100;
        $shareholderModel = loadModel('sh\\Shareholder');
        $row = $shareholderModel->where(['uid' => $spreadid])->find();

        $incomeLog = [];
        $incomeLogTypeEnum = loadEnum('IncomeLogType');
        $incomeLogTypeOptions = $incomeLogTypeEnum->get_options();
        $systemCommission = $commission;
        if($row) {
            $type = $incomeLogTypeEnum->code2key('gudongyijifenyong');
            $fee = floor($commission * 0.6);
            if($fee > 0) {
                $receivers[] = $this->group_buying_user($spreadid, $fee, $incomeLogTypeOptions[$type]);
            }
            $systemCommission -= $fee;
            $incomeLog[] = [
                'uid'    => $spreadid,
                'oid'    => $order['id'],
                'type'   => $type,
                'fee'    => $fee / 100,
                'remark' => $incomeLogTypeOptions[$type]
            ];

            if($row['puid'] > 0) {
                $row = $shareholderModel->where(['uid' => $row['puid']])->find();
                if($row && $row['lv'] >= 1) {
                    $type = $incomeLogTypeEnum->code2key('gudongerjifenyong');
                    $fee = floor($commission * 0.15);
                    if($fee > 0) {
                        $receivers[] = $this->group_buying_user($row['puid'], $fee, $incomeLogTypeOptions[$type]);
                    }
                    $systemCommission -= $fee;
                    $incomeLog[] = [
                        'uid'    => $row['puid'],
                        'oid'    => $order['id'],
                        'type'   => $type,
                        'fee'    => $fee / 100,
                        'remark' => $incomeLogTypeOptions[$type]
                    ];

                    if($row['puid'] > 0) {
                        $row = $shareholderModel->where(['uid' => $row['puid']])->find();
                        if($row && $row['lv'] >= 2) {
                            $type = $incomeLogTypeEnum->code2key('gudongsanjifenyong');
                            $fee = floor($commission * 0.075);
                            if($fee > 0) {
                                $receivers[] = $this->group_buying_user($row['puid'], $fee, $incomeLogTypeOptions[$type]);
                            }
                            $systemCommission -= $fee;
                            $incomeLog[] = [
                                'uid'    => $row['puid'],
                                'oid'    => $order['id'],
                                'type'   => $type,
                                'fee'    => $fee / 100,
                                'remark' => $incomeLogTypeOptions[$type]
                            ];
                        }
                    }
                }
            }
        }

        $type = $incomeLogTypeEnum->code2key('xitongshouru');
        $receivers[] = [
            'type'              => 'MERCHANT_ID',
            'receiver_account'  => $system['sub_mch_id'],
            'amount'            => $systemCommission,
            'description'       => $incomeLogTypeOptions[$type]
        ];
        $incomeLog[] = [
            'uid'    => 0,
            'oid'    => $order['id'],
            'type'   => $type,
            'fee'    => $systemCommission / 100,
            'remark' => $incomeLogTypeOptions[$type]
        ];

        $type = $incomeLogTypeEnum->code2key('shangpinxiaoshoushouru');
        $store = loadModel('tysj\\Store')->find($order['sid']);
        if(empty($store)) {
            throw new \Exception('店铺不存在');
        }
        $incomeLog[] = [
            'uid'    => $store['uid'],
            'oid'    => $order['id'],
            'type'   => $type,
            'fee'    => $order['total_fee'] - $order['commission'],
            'remark' => $incomeLogTypeOptions[$type]
        ];
        $this->multiple($order, $receivers, $incomeLog);
	}

	
}